package com.pinusmusic.service.impl;

import com.pinusmusic.dao.UserDao;
import com.pinusmusic.dao.impl.UserDaoImpl;
import com.pinusmusic.domain.PageBean;
import com.pinusmusic.domain.Singer;
import com.pinusmusic.domain.User;
import com.pinusmusic.service.MusicListService;
import com.pinusmusic.service.UserService;
import com.pinusmusic.util.MailUtils;
import com.pinusmusic.util.UuidUtil;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.logging.SocketHandler;

public class UserServiceImpl implements UserService {
    UserDao ud = new UserDaoImpl();

    //2.0lwj
    @Override
    public User findUserByUid(int uid) {
        return ud.findUserById(uid);
    }


    @Override
    public boolean findUserByUsernameAndUserpassword(String username, String userpassword) {
        return ud.findUserByUsernameAndUserpassword(username,userpassword);
    }

    @Override
    public boolean findUserByUsername(String username) {
        return ud.findUserByUsername(username);
    }

    @Override
    public void insertUser(User user) {
        ud.insertUser(user);
    }

    @Override
    public void updateInformation(User user) {
        ud.updateInformation(user);
    }
    @Override
    public Boolean sendEmail(String name,String email) {
        User user = ud.findByUserName(name);
        if (user!=null){
            if (email.equals(user.getEmail())){
                //生成验证码
                String uuid = UuidUtil.getUuid();
                //将验证码存入数据库
                user.setCode(uuid);
                ud.updateCodeByUsername(uuid,name);
                //发送邮件
                MailUtils.sendMail(user.getEmail(),uuid,"忘记密码验证");
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean compareCode(String code, String email) {
        User u = ud.findByEmail(email);
        if (code.equals(u.getCode())){
            return  true;
        }
        return false;
    }

    @Override
    public Boolean newPwd(String newPwd ,String email) {
        User user = ud.findByEmail(email);
        //判断新密码于旧密码是否相同
        if (newPwd.equals(user.getUserpassword())){
            return false;
        }else {
            ud.updatePwdByEmail(newPwd, email);
        }
        return true;
    }

    @Override
    public void sendVipByEmail(User user) {
        String uuid = UuidUtil.getUuid();
        //将验证码存入数据库
        user.setVipcode(uuid);
        ud.updateVipcodeByUsername(uuid,user.getUsername());
        //发送邮件
        String send="<a href='http://localhost/music/user/active?vipcode="+user.getVipcode()+"' >点击激活</a>";
        MailUtils.sendMail(user.getEmail(),send,"pinusVIP激活");
    }

    @Override
    public boolean active(String vipcode) {
        //1.根据激活码查询对应的用户对象
        User user = ud.findByVipCode(vipcode);
        //2.修改用户的激活状态
        if(user != null){
            ud.updateVipStatus(user);
            return true;
        }
        return false;
    }

    @Override
    public List<User> findUserByName(String username) {
        return ud.findUserByName(username);
    }

    @Override
    public PageBean<User> findUsersByName(int currentPage, int rows, String name) {
        PageBean<User> pageBean = new PageBean<>();
        //2.获取总记录数
        int totalCount = ud.getLikeCount(name);
        pageBean.setTotalCount(totalCount);
        //3.设置总页码数
        int totalPage = totalCount%rows==0?(totalCount/rows):(totalCount/rows)+1;
        pageBean.setTotalPage(totalPage);
        //4.设置当前页码数,设置边界值
        if (currentPage<=0) currentPage=1;
        if (currentPage>=totalPage && totalPage>0) currentPage=totalPage;
        pageBean.setCurrentPage(currentPage);
        //5.设置每页查询的条数
        pageBean.setPageSize(rows);
        //6.设置对应数据
        int start =(currentPage-1)*rows;
        List<User> userList = ud.findUsersByName(start, rows, name);
        pageBean.setList(userList);

        return pageBean;
    }

    @Override
    public User findUserById(Integer uid) {
        return ud.findUserById(uid);
    }
}
